//
//  HJRegisterController.m
//  KaiChat
//
//  Created by lx on 2023/10/17.
//

#import "HJRegisterController.h"
#import "HJTitleTextField.h"
#import "HJSendCodeController.h"
#import "HJMinePresent.h"
#import "QMUIButton.h"
#import "HJWebViewController.h"
#import "YYText.h"
#import "HJTimer.h"
#import "HJResetPasswordController.h"

@interface HJRegisterController ()

@property (nonatomic, weak) HJStyle1Button *registerButton;
@property (nonatomic, weak) QMUIButton *sendCodeButton;
@property (nonatomic, weak) HJTitleTextField *codeTextField;
@property (nonatomic, strong) HJTimer *timer;

@end

@implementation HJRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.lineView.hidden = YES;
    
    UILabel *titleLabel = [UILabel lableWithSuperView:self.view font:UIFontMediumMake(20) color:UIColor.tj_333333Color title:@"Register Kai" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight + 12);
        make.left.offset(leftRightMargin);
    }];
    
    UILabel *subTitleLabel = [UILabel lableWithSuperView:self.view font:UIFontMake(12) color:UIColor.tj_868391Color title:[NSString stringWithFormat:@"Please insert the KAl verifcation code received in the email associated with %@", _email] textAlignment:NSTextAlignmentLeft];
    subTitleLabel.numberOfLines = 0;
    [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(textSubTextLabelMargin);
        make.left.equalTo(titleLabel);
        make.right.inset(leftRightMargin);
    }];
    
    HJTitleTextField *codeTextField = [HJTitleTextField addToView:self.view];
    codeTextField.titleLabel.text = @"Verification code";
    codeTextField.textField.placeholder = @"Verification code";
    codeTextField.textField.font = UIFontMake(14);
    codeTextField.textField.maximumTextLength = 6;
    codeTextField.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    codeTextField.textField.keyboardType = UIKeyboardTypeNumberPad;
    [codeTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.equalTo(subTitleLabel.mas_bottom).offset(40);
        make.height.offset(72);
    }];

    QMUIButton *sendCodeButton = [QMUIButton buttonWithSuperView:codeTextField font:UIFontMake(14) color:UIColor.tj_mainColor title:@"Send"];
    [sendCodeButton setTitleColor:UIColor.tj_BEBDCAColor forState:UIControlStateDisabled];
    [sendCodeButton sizeToFit];
    CGFloat sendCodeButtonW = sendCodeButton.bounds.size.width + 10;
    [sendCodeButton addTarget:self action:@selector(didClickSendCodeButton) forControlEvents:UIControlEventTouchUpInside];
    [codeTextField.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.inset(sendCodeButtonW + 15);
    }];
    [sendCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(10);
        make.width.offset(sendCodeButtonW);
        make.top.bottom.equalTo(codeTextField.textField);
    }];
    
    HJStyle1Button *registerButton = [HJStyle1Button buttonWithSuperView:self.view font:UIFontMediumMake(14) color:nil title:@"Register"];
    registerButton.enabled = NO;
    [registerButton addTarget:self action:@selector(didClickRegisterButton) forControlEvents:UIControlEventTouchUpInside];
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(20);
        make.height.offset(buttonHeight);
        make.top.equalTo(codeTextField.mas_bottom).offset(30);
    }];

    _registerButton = registerButton;
    _codeTextField = codeTextField;
    _sendCodeButton = sendCodeButton;
}

- (void)startCountdown {
    TJWeakify(self);
    [self.timer start:60 countdownOngoing:^(NSInteger time) {
        weakself.sendCodeButton.enabled = NO;
        [weakself.sendCodeButton setTitle:[NSString stringWithFormat:@"%lus", time] forState:UIControlStateDisabled];
    } countdownEnd:^{
        weakself.sendCodeButton.enabled = YES;
        [weakself.sendCodeButton setTitle:@"Send" forState:UIControlStateNormal];
    }];
}


- (void)textFieldDidChange:(UITextField *)textField {
    _registerButton.enabled = !isEmptyString(textField.text);
}

- (void)didClickRegisterButton {
    [self.view endEditing:YES];
    NSString *code = _codeTextField.textField.text;
    [HJHUD showLoadingInView:self.view];
    [HJMinePresent emailCodeLoginWithEmail:_email code:code complete:^(NSError * _Nullable error) {
        if (!error) {
            dispatch_after_main_queue(0.3, ^{
                [HJHUD showSucceedMessage:@"Login successful"];
            });
        }
    }];
}

- (void)didClickSendCodeButton {
    [HJHUD showLoadingInView:self.view];
    [HJMinePresent sendRegisterEmailCode:_email complete:^(NSError * _Nullable error) {
        if (!error) {
             [self startCountdown];
        }
    }];
}

- (HJTimer *)timer {
    if (!_timer) {
        _timer = [HJTimer timer];
    }
    return _timer;
}

 
 
@end
